package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.instance.entity.ReportAttachment;
import com.a01_2021.thousandstimes.instance.mapper.ReportAttachmentMapper;
import com.a01_2021.thousandstimes.instance.service.IReportAttachmentService;
import com.a01_2021.thousandstimes.utils.IO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class ReportAttachmentServiceImpl extends ServiceImpl<ReportAttachmentMapper, ReportAttachment> implements IReportAttachmentService {

    @Resource
    ReportAttachmentMapper reportAttachmentMapper;

    private List<String> expectExt;//附件上传接口后缀名校验

    @Value("${path.reportAttachmentFolder}")
    private String attachmentFolder;//附件存储文件夹名

    public ReportAttachmentServiceImpl() {
        expectExt = new ArrayList<>();
        expectExt.add(".jpg");
        expectExt.add(".png");
        expectExt.add(".dox");
        expectExt.add(".docx");

    }

    /**
     * 将附件全部插入表中
     *
     * @param id         用户id
     * @param attachment 附件集合
     */
    @Override
    public void addAttachment(Long id, ArrayList<String> attachment) {
        for (String item : attachment) {
            if (StringUtils.isEmpty(attachment)) {
                //若url为空则跳过
                continue;
            }
            ReportAttachment reportAttachment = new ReportAttachment();
            reportAttachment.setReportId(id);
            reportAttachment.setAttachmentUrl(item);
            reportAttachmentMapper.insert(reportAttachment);
        }
    }

    /**
     * 通过报告id获取附件url集合
     *
     * @param reportId 报告id
     * @return List<String>附件URL集合
     */
    @Override
    public List<String> getReportAttachment(long reportId) {
        QueryWrapper<ReportAttachment> reportAttachmentQueryWrapper = new QueryWrapper<>();
        reportAttachmentQueryWrapper.eq("report_id", reportId);
        List<ReportAttachment> reportAttachmentList = reportAttachmentMapper.selectList(reportAttachmentQueryWrapper);
        //将类中url提取出来
        return reportAttachmentList.stream().map(ReportAttachment::getAttachmentUrl).collect(Collectors.toList());
    }

    /**
     * 用于上传附件文件
     *
     * @param file       文件名
     * @param instanceId 文件id
     * @return 文件相对路径
     * @throws CustomException 文件后缀名不符合要求时抛出
     * @throws IOException     IO异常
     */
    @Override
    public String upload(MultipartFile file, String instanceId) throws CustomException, IOException {
        String ext = IO.getExt(file, expectExt);
        String fileUrl = IO.saveFile(file, ext, "/" + attachmentFolder + "/" + instanceId);
        return "/report/" + instanceId + "/" + fileUrl;
    }
}
